﻿var organizationlist = {
    init: function () {
        $('#tableid').treegrid({
            fit: true,
            nowrap: false,
            border: false,
            rownumbers: true,
            animate: true,
            toolbar: toolbar,
            //collapsible: true,
            idField: 'ORGANIZATION_ID',
            treeField: 'ORGANIZATION_NAME',
            columns: [[
					{ title: lang.Organization_structure, field: 'ORGANIZATION_NAME', width: 230},
					{ title: lang.Unit_level, field: 'ITEMNAME', width: 190, align: 'center' },
					{ title: lang.Department_head, field: 'ORGANIZATION_MANAGER', width: 100, align: 'center' },
//					{ title: '联系电话', field: 'ORGANIZATION_INNERPHONE', width: 140, align: 'center' },
//                  { title: '传真号码', field: 'ORGANIZATION_FAX', width: 100 },
//					{ title: '邮政区号', field: 'ORGANIZATION_ZIPCODE', width: 140, align: 'center' },
                    {title: lang.sequence, field: 'SORTCODE', width: 80, align: 'center' },
					{ title: lang.Remarks, field: 'ORGANIZATION_REMARK', width: 330, align: 'center' }
				]]
        });
        loadOrgData();
    }
        ,
    search: function () { loadOrgData(); }
}

var _orgdata = [];

    var toolbar = [{
        text: lang.add,
        iconCls: 'icon-add',
        handler: function () { openPage("organizationedit", "", "", lang.add_new_branch); }
    }, '-', {
        text: lang.modify,
        iconCls: 'icon-edit',
        handler: function () {
            var obj = $('#tableid').treegrid('getSelected');
            if (obj) {
                openPage("organizationedit", "orgid=" + obj.ORGANIZATION_ID, "", lang.modify_branch);
            }
            else {
                $.alert(lang.select_branch);
            }
        }
    }, '-', {
        text: lang.delete_,
        iconCls: 'icon-remove',
        handler: function () {
            var obj = $('#tableid').treegrid('getSelected');
            if (obj) {
                var _orgid = obj.ORGANIZATION_ID;
                var isParent = false;
                for (var i = 0; i < _orgdata.length; i++) {
                    if (_orgdata[i].PARENTID == _orgid) {
                        isParent = true;
                    }
                }
                if (isParent) { $.alert(lang.First_remove_the_child_nodes); }
                else {
                    $.extend($.messager.defaults, {
                        ok: lang.ok,
                        cancel: lang.cancel
                    });
                    $.confirm(lang.sure_to_delete_the_current_branch, lang.delete_, function (b) {
                        if (b) {
                            deleteorg(_orgid);
                        }
                    });
                }
            }
            else {
                $.alert(lang.select_branch);
            }
        }
    }];

    //#region 加载菜单数据
    function loadOrgData() {
        $.ajaxHandler({
            url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
            urlType: "QueryOrganizeList",
            success: function (data) {
                if (data && data.length > 0) {
                    var _data = [];
                    for (var i = 0; i < data.length; i++) {
                        if (data[i].PARENTID == '0') {
                            var obj = {};
                            obj.ORGANIZATION_ID = data[i].ORGANIZATION_ID;
                            obj.ORGANIZATION_CODE = data[i].ORGANIZATION_CODE;
                            
                            obj.ORGANIZATION_NAME = data[i].ORGANIZATION_NAME;
                            obj.ORGANIZATION_INNERPHONE = data[i].ORGANIZATION_INNERPHONE;
                            obj.ORGANIZATION_OUTERPHONE = data[i].ORGANIZATION_OUTERPHONE;
                            obj.ORGANIZATION_MANAGER = data[i].ORGANIZATION_MANAGER
                            obj.ORGANIZATION_FAX = data[i].ORGANIZATION_FAX;
                            obj.ORGANIZATION_ZIPCODE = data[i].ORGANIZATION_ZIPCODE;
                            obj.ORGANIZATION_ADDRESS = data[i].ORGANIZATION_ADDRESS;
                            obj.PARENTID = data[i].PARENTID;
                            obj.ORGANIZATION_REMARK = data[i].ORGANIZATION_REMARK;
                            obj.SORTCODE = data[i].SORTCODE;
                            obj.DELETEMARK = data[i].DELETEMARK;
                            obj.ITEMNAME = data[i].ITEMNAME;
                            getsubOrg(data, data[i].ORGANIZATION_ID, obj);
                            _data.push(obj);
                        }
                        _orgdata = data;
                    }
                    $('#tableid').treegrid('loadData', _data);
                }

            }
        });

        function getsubOrg(_d, _pid, obj2) {
            if (_d && _d.length > 0) {
                var _sdata = [];
                for (var i = 0; i < _d.length; i++) {
                    if (_d[i].PARENTID == _pid) {
                        var obj = {};
                        obj.ORGANIZATION_ID = _d[i].ORGANIZATION_ID;
                        obj.ORGANIZATION_CODE = _d[i].ORGANIZATION_CODE;

                        obj.ORGANIZATION_NAME = _d[i].ORGANIZATION_NAME;
                        obj.ORGANIZATION_INNERPHONE = _d[i].ORGANIZATION_INNERPHONE;
                        obj.ORGANIZATION_OUTERPHONE = _d[i].ORGANIZATION_OUTERPHONE;
                        obj.ORGANIZATION_MANAGER = _d[i].ORGANIZATION_MANAGER
                        obj.ORGANIZATION_FAX = _d[i].ORGANIZATION_FAX;
                        obj.ORGANIZATION_ZIPCODE = _d[i].ORGANIZATION_ZIPCODE;
                        obj.ORGANIZATION_ADDRESS = _d[i].ORGANIZATION_ADDRESS;
                        obj.PARENTID = _d[i].PARENTID;
                        obj.ORGANIZATION_REMARK = _d[i].ORGANIZATION_REMARK;
                        obj.SORTCODE = _d[i].SORTCODE;
                        obj.DELETEMARK = _d[i].DELETEMARK;
                        obj.ITEMNAME = _d[i].ITEMNAME;
                        _sdata.push(obj);
                    }
                }
                obj2.children = _sdata;
                if (_sdata.length > 0) {
                    for (var j = 0; j < _sdata.length; j++) {
                        getsubOrg(_d, _sdata[j].ORGANIZATION_ID, _sdata[j]);
                    }
                }
            }
        }
    }

    //#endregion

    //#region 删除部门
    function deleteorg(ORGANIZATION_ID) {
        $.ajaxHandler({
            url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
            urlType: "EditOrganizationByID",
            model: function () { return $.toJSON({ 'action': 0, 'ORGANIZATION_ID': ORGANIZATION_ID }); },
            success: function (data) {
                $.alert(lang.Successfully_deleted, null, function () { organizationlist.search(); });
            }
        });
    }
    //#endregion